<?php
/**
 * Created by PhpStorm.
 * User: user1
 * Date: 2018/5/21
 * Time: 18:01
 */

namespace app\api\model;


use think\Exception;

class Banner extends BaseModel
{


    /**
     * 创建或者编辑banner
     * @param $param
     * @param $id
     * @return array|\think\response\Json
     */
    public static function createOrUpdateBanner($param, $id) {

        try {
            if ($id > 0) {

                $banner = self::get($id);
                if (!$banner) {
                    return json(['code' => 1, 'msg' => 'banner不存在']);
                }

                $check_name = self::where([['name', 'eq', $param['name']], ['id', 'neq', $id]])->find();
                if ($check_name) {
                    return ['code' => 1, 'msg' => 'banner名称已存在!'];
                }

                $banner->save($param);

            } else {
                $check_name = self::where([['name', 'eq', $param['name']]])->find();
                if ($check_name) {
                    return ['code' => 1, 'msg' => 'banner名称已存在!'];
                }

                self::create($param);
            }
            return ['code' => 0, 'msg' => '保存成功'];
        } catch (Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
}